package net.ds0101.FindTen;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.MotionEvent;
import net.ds0101.framework.android.neox.core.NXEventHandler;
import net.ds0101.framework.android.neox.core.NXMenuItemActionHandler;
import net.ds0101.framework.android.neox.core.NXNode;
import net.ds0101.framework.android.neox.core.NXNode.OnClickCallback;
import net.ds0101.framework.android.neox.main.NXDirector;
import net.ds0101.framework.android.neox.main.NXScene;
import net.ds0101.framework.android.neox.util.NXResourceConverter;
import net.ds0101.framework.android.neox.widget.NXBitmapSheet;
import net.ds0101.framework.android.neox.widget.NXMenu;
import net.ds0101.framework.android.neox.widget.NXMenuItem;
import net.ds0101.framework.android.neox.widget.NXMenuItemImage;
import net.ds0101.framework.android.neox.widget.NXSprite;

public class MainMenu extends NXScene {
	private final static int TOTAL_LEVEL = 3;
	public final static String IS_MUTE = "mute";
	private NXMenuItem[] mLevelButton = new NXMenuItem[TOTAL_LEVEL];
	private NXMenu mMenu;
	private final static int BUTTON_MARGIN = (int) (NXDirector.getScreenHeight() / 18);
	private NXScene mGameScene;

	public MainMenu(Context context) {
		super(context);
    	
		int levelButtonId[] = {R.drawable.btn2x2, R.drawable.btn3x3, R.drawable.btn4x4};
		NXSprite title = NXSprite.spriteWithBitmap(NXResourceConverter.getBitmapFromResource(this, R.drawable.title));
		title.position(NXDirector.getScreenWidth() / 2, NXDirector.getScreenHeight() / 5);
		addChild(title);
		
		mGameScene = new GameScene(context);
		for (int i = 0; i < TOTAL_LEVEL; ++i) {
			final int CUREENT_LEVEL = i;
			Bitmap buttonSkin = NXResourceConverter.getBitmapFromResource(this, levelButtonId[i]);
			mLevelButton[i] = 
				new NXMenuItemImage(buttonSkin, buttonSkin, buttonSkin, null);
			mLevelButton[i].setOnClickListener(new OnClickCallback() {
				@Override
				public void onClick(NXNode node) {
					((MainEntry)director()).gameGrid(CUREENT_LEVEL + 2, CUREENT_LEVEL + 2);
					director().pushScene(mGameScene);
				}
			});
			mLevelButton[i].position(0, i * BUTTON_MARGIN + i * buttonSkin.getHeight());
		}
		
		mMenu = new NXMenu(mLevelButton);
		mMenu.position(NXDirector.getScreenWidth() / 2, (int) (NXDirector.getScreenHeight() / 2.4));
		//mMenu.addItem(mLevelButton);
		addChild(mMenu, 1);
		
		Bitmap closeSkin = NXResourceConverter.getBitmapFromResource(this, R.drawable.close);
		if (null != closeSkin) {
			NXSprite closeBtn = NXSprite.spriteWithBitmap(closeSkin);
			closeBtn.position(NXDirector.getScreenWidth() - closeBtn.width() / 2, closeBtn.height() / 2);
			closeBtn.setOnClickListener(new OnClickCallback() {
				@Override
				public void onClick(NXNode node) {
					Runnable task = new Runnable() {
						
						@Override
						public void run() {
							android.os.Process.killProcess(android.os.Process.myPid());
						}
					};
					
					Handler handler = new Handler();
					handler.postDelayed(task, 500); // avoid unhandle exception in android 2.1
					((NXDirector)getContext()).finish();
				}
			});
			addChild(closeBtn);
		}
		
		Bitmap muteSkin = NXResourceConverter.getBitmapFromResource(this, R.drawable.mute);
		if (null != muteSkin) {
			final NXSprite muteBtn = new NXSprite(new NXBitmapSheet(muteSkin, 2, 1));
			muteBtn.position(NXDirector.getScreenWidth() - (muteBtn.width() + (muteBtn.width() >> 1) + 5), muteBtn.height() >> 1);
			SharedPreferences data = ((NXDirector)this.getContext()).getPreferences(Activity.MODE_PRIVATE);
			muteBtn.setDisplayFrame(data.getInt(IS_MUTE, 0), 0);
			
			muteBtn.setOnClickListener(new OnClickCallback() {
				@Override
				public void onClick(NXNode node) {
					SharedPreferences data = ((NXDirector)MainMenu.this.getContext()).getPreferences(Activity.MODE_PRIVATE);
					int mute = data.getInt(IS_MUTE, 0) == 0 ? 1 : 0; // switch mute state.
					muteBtn.setDisplayFrame(mute, 0);
					data.edit().putInt(IS_MUTE, mute).commit();
				}
			});
			addChild(muteBtn);
		}
	}
	
	/*public void onEnter() {
		super.onEnter();
		int levelButtonId[] = {R.drawable.btn2x2, R.drawable.btn3x3, R.drawable.btn4x4};
		NXSprite title = NXSprite.spriteWithBitmap(NXResourceConverter.getBitmapFromResource(this, R.drawable.title));
		title.position(NXDirector.getScreenWidth() / 2, NXDirector.getScreenHeight() / 5);
		addChild(title);
		for (int i = 0; i < TOTAL_LEVEL; ++i) {
			final int CUREENT_LEVEL = i;
			Bitmap buttonSkin = NXResourceConverter.getBitmapFromResource(this, levelButtonId[i]);
			mLevelButton[i] = 
				new NXMenuItemImage(buttonSkin, buttonSkin, buttonSkin, null, new NXMenuItemActionHandler() {
					@Override
					public void onTapped(NXNode sender) {
						director().replaceWithScene(new GameScene(getContext(), 
								CUREENT_LEVEL + 2, CUREENT_LEVEL + 2));
					}
					
					@Override
					public void onPressed(NXNode sender) { }
					
					@Override
					public void onReleased(NXNode sender) { }
				});
			mLevelButton[i].position(0, i * BUTTON_MARGIN + i * buttonSkin.getHeight());
		}
		
		mMenu = new NXMenu(mLevelButton);
		mMenu.position(NXDirector.getScreenWidth() / 2, (int) (NXDirector.getScreenHeight() / 2.6));
		addChild(mMenu, 1);
		
		Bitmap closeSkin = NXResourceConverter.getBitmapFromResource(this, R.drawable.close);
		if (null != closeSkin) {
			NXSprite closeBtn = NXSprite.spriteWithBitmap(closeSkin);
			closeBtn.position(NXDirector.getScreenWidth() - closeBtn.width() / 2, closeBtn.height() / 2);
			closeBtn.setEventHandler(new NXEventHandler() {
				@Override
				public boolean onTouchEvent(MotionEvent event) {
					((NXDirector)getContext()).finish();
					return true;
				}
			});
			addChild(closeBtn);
		}
	}*/

	@Override
	public void onScreenSizeChanged(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(int arg0) {
		// TODO Auto-generated method stub

	}

	/*@Override
	public boolean onTouchEvent(int action, float x, float y) {
		boolean ret = super.onTouchEvent(event);
		//director().replaceWithScene(new GameScene(getContext(), 4, 4));
		return ret;
	}*/
}
